/**
 * HomeCtrl.java
 *
 */
package com.navidove.front.web.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.navidove.appspotlight.model.bo.AppAdItem;
import com.navidove.appspotlight.model.bo.AppDevice;
import com.navidove.appspotlight.model.bo.AppDictionary;
import com.navidove.appspotlight.model.bo.AppItem;
import com.navidove.appspotlight.model.service.AppAdService;
import com.navidove.appspotlight.model.service.AppDictionaryService;
import com.navidove.appspotlight.model.service.AppItemService;
import com.navidove.appspotlight.model.service.FrontService;
import com.navidove.appspotlight.model.service.SysParamService;

/**
 * HomeCtrl
 *
 * @author $Author$
 * @version $Revision$
 * @date $Date$
 * @url $URL$
 */
@Controller
public class HomeCtrl {
	
	@Autowired
	private FrontService frontService;
	
	@Autowired
	private AppItemService appItemService;
	
	@Autowired
	private AppAdService appAdService;
	
	@Autowired
	private AppDictionaryService appDictionaryService;
	
	@Autowired
	private SysParamService sysParamService;

	@RequestMapping(value="/index")
	public String index(Model model) {

		Map<Long, AppDictionary> sourceMap = new HashMap<Long, AppDictionary>();
		for (AppDictionary dic : appDictionaryService.getAppDictionaryList(AppDictionary.DICTIONARY_TYPE_SOURCE)) {
			sourceMap.put(dic.getId(), dic);
		}
	
		
		List<AppDictionary> sourceList = new ArrayList<AppDictionary>();
		List<Map<String,Object>> appItemMapList = frontService.getAppItemMapList();		
		Map<Long, List<Map<String,Object>>> appItemMap = new HashMap<Long, List<Map<String,Object>>>();
		for (Map<String,Object> item : appItemMapList) {
			Long sourceId = (Long) item.get("sourceId");
			
			List<Map<String,Object>> list = appItemMap.get(sourceId);
			if (list == null) {
				list = new ArrayList<Map<String,Object>>();
				appItemMap.put(sourceId, list);
				sourceList.add(sourceMap.get(sourceId));
			}
			list.add(item);
		}
		
		model.addAttribute("sourceList", sourceList);
		model.addAttribute("appItemMap", appItemMap);
		
		List<AppAdItem> appAdItemList = appAdService.getAppAdItemList(Boolean.TRUE, AppAdItem.AD_TYPE_AD);
		model.addAttribute("appAdItemList", appAdItemList);
		
		List<AppAdItem> hotList = appAdService.getAppAdItemList(Boolean.TRUE, AppAdItem.AD_TYPE_HOT);
		model.addAttribute("hotList", hotList);
		
		Map<String, String> sysParamMap = sysParamService.getStringParamMap();
		model.addAttribute("sysParamMap", sysParamMap);
		
		return "index";
	}	 
	
	@RequestMapping(value="/app{appId}")
	public String lightbox(Model model,
			@PathVariable("appId") Long id) {
		
		AppItem appItem  = appItemService.getAppItem(id);
		model.addAttribute("appItem", appItem);
		
		model.addAttribute("android", AppDevice.DeviceType.ANDROID);
		model.addAttribute("ios", AppDevice.DeviceType.IOS);
		
		return "lightbox-both";
	}
	
	@RequestMapping(value="/privacy")
	public String privacy(Model model) {
		return "privacy";
	}
}
